/**
 * @(#)Logo.java 1.0 00/09/14
 * @author Shabda and Ashwini
 *This is the main class required for logo
 *it just creates a JPanel which contains the drawArea and buttons
 *to manipulate the draw area
 *
 */
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class Logo{
	public Logo() {
	}
	public JPanel createButtonsNText(){
		final DrawArea dw=new DrawArea();
		JButton fd=new JButton("FD");
		final JTextField fdt=new JTextField(3);
		JButton bk=new JButton("BK");
		final JTextField bkt=new JTextField(3);
		JButton rt=new JButton("RT");
		final JTextField rtt=new JTextField(3);
		JButton lt=new JButton("LT");
		final JTextField ltt=new JTextField(3);
		JButton pd=new JButton("PD");
		JButton pe=new JButton("PE");
		JButton pu=new JButton("PU");
		JButton home=new JButton("HOME");
		JLabel label=new JLabel("LOGO>");
		label.setBackground(Color.white);
		final JTextField textf=new JTextField("enter your commands here",50);
		final JTextArea text=new JTextArea("LOGO>",6,55);
		textf.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				java.util.List l=StringWorker.lastCommandArr(textf.getText());
				UtilityWorker.doIt(dw,l);
				System.out.println("enter");
				String s=text.getText();
				s+=textf.getText();
				s+="\nLOGO>";
				text.setText(s);
				textf.setText("");
				}
			});
		textf.addFocusListener(new FocusListener(){

			public void focusGained(FocusEvent arg0) {
				textf.setText("");
				
			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				
			}});
		text.setLineWrap(true);
		text.setWrapStyleWord(true);
		JScrollPane areaScrollPane = new JScrollPane(text);
        areaScrollPane.setVerticalScrollBarPolicy(
		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        areaScrollPane.setPreferredSize(new Dimension(600,70));
		home.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
			    dw.home();
			    JButton c=(JButton)e.getSource();
			    UtilityWorker.updateText(text,c);
				}
			});
		pd.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				dw.t.pen=0;
				JButton c=(JButton)e.getSource();
			    UtilityWorker.updateText(text,c);
				}
			});
		pe.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				dw.t.pen=1;
				JButton c=(JButton)e.getSource();
			    UtilityWorker.updateText(text,c);
				}
				});
		pu.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				dw.t.pen=2;
				JButton c=(JButton)e.getSource();
			    UtilityWorker.updateText(text,c);
				}
				});
		fd.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String s=fdt.getText();
				dw.forward(Integer.parseInt(s));
				String s1=text.getText();
				s1+="FD ";
				s1+=s;
				s1+="\nLOGO>";
				text.setText(s1);
				}
			});
		bk.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String s=bkt.getText();
				dw.backward(Integer.parseInt(s));
				String s1=text.getText();
				s1+="BK ";
				s1+=s;
				s1+="\nLOGO>";
				text.setText(s1);
				}
			});
		rt.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String s=rtt.getText();
				dw.rightTurn(Integer.parseInt(s));
				String s1=text.getText();
				s1+="RT ";
				s1+=s;
				s1+="\nLOGO>";
				text.setText(s1);
				}
			});
		lt.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String s=ltt.getText();
				dw.leftTurn(Integer.parseInt(s));
				String s1=text.getText();
				s1+="LT ";
				s1+=s;
				s1+="\nLOGO>";
				text.setText(s1);
				}
			});
		fdt.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String s=fdt.getText();
				dw.forward(Integer.parseInt(s));
				String s1=text.getText();
				s1+="FD ";
				s1+=s;
				s1+="\nLOGO>";
				text.setText(s1);
				}
			});
		bkt.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String s=bkt.getText();
				dw.backward(Integer.parseInt(s));
				String s1=text.getText();
				s1+="BK ";
				s1+=s;
				s1+="\nLOGO>";
				text.setText(s1);
				}
			});
		rtt.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String s=rtt.getText();
				dw.rightTurn(Integer.parseInt(s));
				String s1=text.getText();
				s1+="RT ";
				s1+=s;
				s1+="\nLOGO>";
				text.setText(s1);
				}
			});
		ltt.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String s=ltt.getText();
				dw.leftTurn(Integer.parseInt(s));
				String s1=text.getText();
				s1+="LT ";
				s1+=s;
				s1+="\nLOGO>";
				text.setText(s1);
				}
			});
		GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
		JPanel pane=new JPanel();
		pane.setLayout(gridbag);
		c.gridx=0;
		c.gridy=0;
		c.gridwidth=12;
		c.gridheight=4;
		gridbag.setConstraints(dw, c);
		pane.add(dw);
		c.gridx=0;
		c.gridy=4;
		c.gridwidth=1;
		c.gridheight=1;
		gridbag.setConstraints(fd, c);
		pane.add(fd);
		c.gridx=1;
		c.gridy=4;
		pane.add(fdt);
		gridbag.setConstraints(fdt, c);
		c.gridx=2;
		c.gridy=4;
		gridbag.setConstraints(bk, c);
		pane.add(bk);
		c.gridx=3;
		c.gridy=4;
		gridbag.setConstraints(bkt, c);
		pane.add(bkt);
		c.gridx=4;
		c.gridy=4;
		gridbag.setConstraints(rt, c);
		pane.add(rt);
		c.gridx=5;
		c.gridy=4;
		gridbag.setConstraints(rtt, c);
		pane.add(rtt);
		c.gridx=6;
		c.gridy=4;
		gridbag.setConstraints(lt, c);
		pane.add(lt);
		c.gridx=7;
		c.gridy=4;
		gridbag.setConstraints(ltt, c);
		pane.add(ltt);
		c.gridx=8;
		c.gridy=4;
		gridbag.setConstraints(pd, c);
		pane.add(pd);
		c.gridx=9;
		c.gridy=4;
		gridbag.setConstraints(pe, c);
		pane.add(pe);
		c.gridx=10;
		c.gridy=4;
		gridbag.setConstraints(pu, c);
		pane.add(pu);
		c.gridx=11;
		c.gridy=4;
		gridbag.setConstraints(home, c);
		pane.add(home);
		c.gridx=0;
		c.gridy=5;
		gridbag.setConstraints(label, c);
		pane.add(label);
		c.gridx=1;
		c.gridy=5;
		c.gridwidth=12;
		c.gridheight=1;
        gridbag.setConstraints(textf, c);
        pane.add(textf);
		c.gridx=0;
		c.gridy=6;
		c.gridwidth=12;
		c.gridheight=5;
        gridbag.setConstraints(areaScrollPane, c);
        pane.add(areaScrollPane);
		return pane;
		}

	public static void main(String args[]) {
		System.out.println("Starting Logo...");
		Logo app=new Logo();
		JFrame frame=new JFrame("Logo");
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we){
				System.exit(0);
			}
		});
		JPanel pane=app.createButtonsNText();
		frame.getContentPane().add(pane);
		frame.pack();
		frame.show();
	}
}
